﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Nizm0.QOT.Library.Forms
{
    public class MyMdiChild : WeifenLuo.WinFormsUI.Docking.DockContent//, IMDIChildTabControl
    {
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                //_tabPag.Text = value;
                base.Text = value;
            }
        }

        public MyMdiChild()
        : base()
        {
            //InitializeComponent();

            base.Activated += new EventHandler(MyMdiChild_Activated);
            base.Disposed += new EventHandler(MyMdiChild_Disposed);
        }
        public MyMdiChild(ref Form parent)
        : this()
        {
            //InitializeComponent();
            //this.TopLevel = false;
            //((Form)parent).Controls.Add(this);
            //this.MdiParent = parent;
            //if (_tabCtrl == null)
            //{
            //    foreach (Control tmpcontrol in ((Form)parent).Controls)
            //        if (tmpcontrol.GetType() == typeof(TabControl))
            //            _tabCtrl = (TabControl)tmpcontrol;
            //}
            //this._tabPag = new TabPage();
            //_tabPag.Parent = _tabCtrl;
            //_tabPag.Text = this.Text;
            //base.Show();
            //base.Activated += new EventHandler(MyMdiChild_Activated);
            //base.FormClosing += new FormClosingEventHandler(MyMdiChild_FormClosing);
            //base.Disposed += new EventHandler(MyMdiChild_Disposed);
            //base.Load +=new EventHandler(MyMdiChild_Load);
        }

        void MyMdiChild_Disposed(object sender, EventArgs e)
        {
            Close();
        }

        void MyMdiChild_FormClosing(object sender, FormClosingEventArgs e)
        {
            Close();
        }

        void MyMdiChild_Activated(object sender, EventArgs e)
        {
            Activate();
        }


        public new void Show()
        {
            //if (this._tabPag == null)
            //    this._tabPag = new TabPage();
            //if (!this._tabCtrl.TabPages.Contains(this._tabPag))
            //    _tabCtrl.TabPages.Add(this._tabPag);

            //_tabPag.Parent = _tabCtrl;
            //_tabPag.Text = this.Text;
            //_tabPag.Show();
            //_tabCtrl.Visible = true;
            //AddToolStripToParent();

            base.Show();
        }

        public new void Activate()
        {
            //if (_tabPag == null)
            //{
            //    this._tabPag = new TabPage();
            //    _tabPag.Parent = _tabCtrl;
            //    _tabPag.Text = this.Text;
            //}
            //_tabCtrl.SelectedTab = _tabPag;

            base.Activate();
        }

        public new void Hide()
        {
            if (this.InvokeRequired)
            {
                this.Invoke((EventHandler)delegate
                {
                    this.Visible = false;
                });
            }
            else
                this.Visible = false;
            //this.Close();
            //if (this._tabPag != null && this.TabCtrl != null)
            //    this._tabCtrl.TabPages.Remove(this._tabPag);
            ////.Dispose();

            ////If no Tabpage left
            //if (!_tabCtrl.HasChildren)
            //{
            //    _tabCtrl.Visible = false;
            //}

            //base.Hide();
            //base.Visible = false;
            base.Hide();
        }

        public new void Close()
        {
            //if (this._tabPag != null)
            //    this._tabPag.Dispose();
            ////If no Tabpage left
            //if (!_tabCtrl.HasChildren)
            //{
            //    _tabCtrl.Visible = false;
            //}
            base.Close();
        }

        #region TabControlForChildWindows

        //protected TabControl _tabCtrl;
        //protected TabPage _tabPag;

        //public TabPage TabPag
        //{
        //    get { return _tabPag; }
        //    set { _tabPag = value; }
        //}

        //public TabControl TabCtrl
        //{
        //    get { return _tabCtrl; }
        //    set { _tabCtrl = value; }
        //}

        //public new void Load(EventArgs e)
        //{
        //    base.OnLoad(e);
        //}
        #endregion

        #region Lock MdiClient inside client area
        // Pointer to the mdiparent client area
        //MdiClient m_parentMdiClient = null;

        /// <summary>
        /// Get a pointer to the mdiparent client area
        /// </summary>
        //private void SetParentMdiClient()
        //{
        //    foreach (Control c in this.MdiParent.Controls)
        //    {
        //        if (c is MdiClient)
        //        {
        //            m_parentMdiClient = (MdiClient)c;
        //            break;
        //        }
        //    }
        //}

        //[StructLayoutAttribute(LayoutKind.Sequential)]
        //struct WM_PosChanging
        //{
        //    public int hWnd;
        //    public int hWndInsertAfter;
        //    public int X;
        //    public int Y;
        //    public int cX;
        //    public int cY;
        //    public int Flags;
        //}

        //protected const int WM_WINDOWPOSCHANGING = 0x46;

        //protected override void WndProc(ref Message m)
        //{
        //    // Make sure the m_parentMdiClient has been already assigned
        //    if (m.Msg == WM_WINDOWPOSCHANGING && m_parentMdiClient != null)
        //    {
        //        // Calculate borderwidth in pixels
        //        int borderWidth = this.MdiParent.Bounds.Width - this.MdiParent.ClientRectangle.Width;
        //        borderWidth = borderWidth / 2;

        //        // Get params from message
        //        WM_PosChanging lParam = ((WM_PosChanging)m.GetLParam(typeof(WM_PosChanging)));

        //        // Check left and top
        //        if (lParam.X < 0) lParam.X = 0;
        //        if (lParam.Y < 0) lParam.Y = 0;

        //        // Calculate client area size minus border
        //        int clsize_heigh = m_parentMdiClient.Height - borderWidth;
        //        int clsize_width = m_parentMdiClient.Width - borderWidth;

        //        // Check right and bottom
        //        if (lParam.X + lParam.cX > clsize_width) lParam.X = clsize_width - lParam.cX;
        //        if (lParam.Y + lParam.cY > clsize_heigh) lParam.Y = clsize_heigh - lParam.cY;

        //        // Copy back the structure
        //        Marshal.StructureToPtr(lParam, m.LParam, true);
        //    }
        //    base.WndProc(ref m);
        //}

        //private void MyMdiChild_Load(object sender, EventArgs e)
        //{
        //    // Make sure you have access to mdiParent MdiClient
        //    SetParentMdiClient();
        //}
        #endregion

        public override string ToString()
        {
            if (this.Text.StartsWith("*"))
                return this.Text.Remove(0, 1);
            else
                return this.Text;
        }

        //public void SetActionOnActivate(EventHandler OnActivateEvent)
        //{
        //    this.Activated -= OnActivateEvent;
        //}
        private ToolStripMenuItem fileToolStripMenuItem;
        public ToolStripMenuItem editToolStripMenuItem;
        private ToolStripMenuItem filtrToolStripMenuItem;
        private ToolStripMenuItem queryToolStripMenuItem;
        private ToolStripMenuItem toolsToolStripMenuItem;
        private ToolStripMenuItem testToolStripMenuItem;
        public MenuStrip mainMenu;

        private void InitializeComponent()
        {
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.testToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.editToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.filtrToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.queryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.mainMenu = new System.Windows.Forms.MenuStrip();
            this.mainMenu.SuspendLayout();
            this.SuspendLayout();
            // 
            // fileToolStripMenuItem
            // 
            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.testToolStripMenuItem});
            this.fileToolStripMenuItem.MergeAction = System.Windows.Forms.MergeAction.MatchOnly;
            this.fileToolStripMenuItem.MergeIndex = 1;
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
            this.fileToolStripMenuItem.Text = "&File";
            // 
            // testToolStripMenuItem
            // 
            this.testToolStripMenuItem.MergeAction = System.Windows.Forms.MergeAction.Insert;
            this.testToolStripMenuItem.MergeIndex = 10;
            this.testToolStripMenuItem.Name = "testToolStripMenuItem";
            this.testToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.testToolStripMenuItem.Text = "test";
            // 
            // editToolStripMenuItem
            // 
            this.editToolStripMenuItem.MergeIndex = 2;
            this.editToolStripMenuItem.Name = "editToolStripMenuItem";
            this.editToolStripMenuItem.Size = new System.Drawing.Size(39, 20);
            this.editToolStripMenuItem.Text = "&Edit";
            // 
            // filtrToolStripMenuItem
            // 
            this.filtrToolStripMenuItem.MergeIndex = 3;
            this.filtrToolStripMenuItem.Name = "filtrToolStripMenuItem";
            this.filtrToolStripMenuItem.Size = new System.Drawing.Size(39, 20);
            this.filtrToolStripMenuItem.Text = "Filtr";
            // 
            // queryToolStripMenuItem
            // 
            this.queryToolStripMenuItem.MergeIndex = 10;
            this.queryToolStripMenuItem.Name = "queryToolStripMenuItem";
            this.queryToolStripMenuItem.Size = new System.Drawing.Size(51, 20);
            this.queryToolStripMenuItem.Text = "&Query";
            // 
            // toolsToolStripMenuItem
            // 
            this.toolsToolStripMenuItem.MergeIndex = 10;
            this.toolsToolStripMenuItem.Name = "toolsToolStripMenuItem";
            this.toolsToolStripMenuItem.Size = new System.Drawing.Size(48, 20);
            this.toolsToolStripMenuItem.Text = "&Tools";
            // 
            // mainMenu
            // 
            this.mainMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.editToolStripMenuItem,
            this.filtrToolStripMenuItem,
            this.queryToolStripMenuItem,
            this.toolsToolStripMenuItem});
            this.mainMenu.Location = new System.Drawing.Point(0, 0);
            this.mainMenu.Name = "mainMenu";
            this.mainMenu.Size = new System.Drawing.Size(421, 24);
            this.mainMenu.TabIndex = 0;
            this.mainMenu.Text = "mainMenu";
            this.mainMenu.Visible = false;
            // 
            // MyMdiChild
            // 
            this.ClientSize = new System.Drawing.Size(421, 264);
            this.Controls.Add(this.mainMenu);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            this.MainMenuStrip = this.mainMenu;
            this.Name = "MyMdiChild";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.mainMenu.ResumeLayout(false);
            this.mainMenu.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
    }
}

